/* 
 * Created on 2007-5-12
 * Last modified on 2007-5-12
 * Powered by YeQiangWei.com
 */
package com.yeqiangwei.mail;

import java.security.Security;
import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.apache.log4j.Logger;

import com.yeqiangwei.util.Validator;

public class MailProvider implements Mail{
	
	private static final Logger logger = Logger.getLogger(MailProvider.class.getName());
	
	private String message;

	public boolean send(final MailModel model) {
		if(Validator.isEmpty(model)){
			return false;
		}
		if(Validator.isEmpty(model.getSubject())
				||Validator.isEmpty(model.getContent())
				||Validator.isEmpty(model.getFrom())
				||Validator.isEmpty(model.getTo())
		){
			this.setMessage("标题、内容、收件人、发件人均不能为空！");
			return false;
		}
		boolean bea = false;
		Properties props = System.getProperties();
		props.setProperty("mail.smtp.host", model.getSmtpServer());
		if(model.getNeedSSL()){ //判断是否有SSL安全套接字验证
			Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
			final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
			props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
			props.put("mail.smtp.socketFactory.fallback", "false");
		}
		if(!Validator.isEmpty(model.getSmtport())){ //smtp服务器端口
			props.put("mail.smtp.port", model.getSmtport());
			props.put("mail.smtp.socketFactory.port", model.getSmtport());
		}
		if(model.getNeedAuth()){ //判断SMTP是否需要身份验证
			props.put("mail.smtp.auth","true");
			logger.debug("mail.smtp.auth = true");
		}else{
			props.put("mail.smtp.auth","false");
			logger.debug("mail.smtp.auth = false");
		}
		logger.debug("smtpId:"+model.getSmtpId());
		logger.debug("smtpPassword:"+model.getSmtpPassword());
		Session session = Session.getDefaultInstance(props, new Authenticator(){
		    protected PasswordAuthentication getPasswordAuthentication() {
		    return new PasswordAuthentication(model.getSmtpId(), model.getSmtpPassword());
		}});
		try {   
			MimeMessage msg = new MimeMessage(session);   
			msg.setFrom(new InternetAddress(model.getFrom()));
			msg.setRecipients(Message.RecipientType.TO, 
					  InternetAddress.parse(model.getTo(),false));
            if(!Validator.isEmpty(model.getCc())){
                msg.setRecipients(Message.RecipientType.CC, 
                      InternetAddress.parse(model.getCc(),false));
            }
			msg.setSubject(model.getSubject(),"utf-8");  //设置邮件标题
			msg.setText(model.getContent());//设置邮件内容
			msg.setSentDate(new Date());  //邮件发送日期
			msg.saveChanges();
		   	Transport.send(msg);  //执行发送
            bea = true;
            this.setMessage("发送成功！");
		}catch(MessagingException e){
			this.setMessage("发送失败！");
			logger.error(e.toString());
		}
		return bea;
	}

	public String getMessage() {
		return this.message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

}
